<?php
/**
 * Table options
 * @author muinx
 *
 */
class TableOptionsHelper
{
	/**
	 * Pre-defined fields for table
	 */
	public function defaultFields()
	{
		$arrOptions = array(	'id' 			=> array('type' => 'int', 		'length' => 11, 	'attributes' => 'UNSIGNED', 'collation' => 'utf8_general_ci', 'field_index' => 'primary', 'auto_increment' => true, 'comment' => 'table id'),
								'state' 		=> array('type' => 'tinyint', 	'length' => 3, 		'attributes' => 'UNSIGNED', 'collation' => 'utf8_general_ci', 'field_index' => null, 'auto_increment' => false, 'comment' => ''),
								'ordering' 		=> array('type' => 'int', 		'length' => 11, 	'attributes' => 'UNSIGNED', 'collation' => 'utf8_general_ci', 'field_index' => null, 'auto_increment' => false, 'comment' => ''),
								'metakey' 		=> array('type' => 'text',		'length' => null, 	'attributes' => null, 		'collation' => 'utf8_general_ci', 'field_index' => null, 'auto_increment' => false, 'comment' => ''),
								'metadesc' 		=> array('type' => 'text', 		'length' => null, 	'attributes' => null, 		'collation' => 'utf8_general_ci', 'field_index' => null, 'auto_increment' => false, 'comment' => ''),
								'publish_up' 	=> array('type' => 'datetime', 	'length' => null, 	'attributes' => null, 		'collation' => 'utf8_general_ci', 'field_index' => null, 'auto_increment' => false, 'comment' => ''),
								'publish_down' 	=> array('type' => 'datetime', 	'length' => null, 	'attributes' => null, 		'collation' => 'utf8_general_ci', 'field_index' => null, 'auto_increment' => false, 'comment' => ''),
								'checked_out' 	=> array('type' => 'int', 		'length' => 11, 	'attributes' => 'UNSIGNED', 'collation' => 'utf8_general_ci', 'field_index' => null, 'auto_increment' => false, 'comment' => ''),
								'checked_out_time' => array('type' => 'datetime', 'length' => null, 'attributes' => null, 		'collation' => 'utf8_general_ci', 'field_index' => null, 'auto_increment' => false, 'comment' => ''),
								'created' 		=> array('type' => 'datetime', 	'length' => null, 	'attributes' => null, 		'collation' => 'utf8_general_ci', 'field_index' => null, 'auto_increment' => false, 'comment' => ''),
								'created_by' 	=> array('type' => 'int', 		'length' => 11, 	'attributes' => 'UNSIGNED', 'collation' => 'utf8_general_ci', 'field_index' => null, 'auto_increment' => false, 'comment' => ''),
								'modified' 		=> array('type' => 'datetime', 	'length' => null, 	'attributes' => null,		'collation' => 'utf8_general_ci', 'field_index' => null, 'auto_increment' => false, 'comment' => ''),
								'modified_by' 	=> array('type' => 'int', 		'length' => 11, 	'attributes' => 'UNSIGNED', 'collation' => 'utf8_general_ci', 'field_index' => null, 'auto_increment' => false, 'comment' => ''),
								'language' 		=> array('type' => 'char', 		'length' => 7, 		'attributes' => null, 'collation' => 'utf8_general_ci', 'field_index' => null, 'auto_increment' => false, 'comment' => ''),
								/*
								'' => array('type' => '', 'length' => null, 'field_index' => '', 'auto_increment' => false, 'comment' => ''),
								*/
		
						);
						
		return $arrOptions;
	}
	
	/**
	 * Function to get table storage engine
	 */
	public function storageEngine()
	{
		$options = array();
		
		$arrOptions = array(	'MRG_MYISAM', 
								'MyISAM', 
								'BLACKHOLE', 
								'CSV', 
								'MEMORY', 
								'ARCHIVE', 
								'InnoDB', 
								'PERFORMANCE_SCHEMA'
		);
		
		foreach ($arrOptions as $option)
		{
			$opt = new stdClass();			
			$opt->text = $opt->value = $option;
			
			$options[] = $opt;
		}
		
		return $options;
		
	}
	
	/**
	 * Function to get index option input
	 * @param String $name Select's name
	 * @param String $val Value to selected
	 * 
	 * @return Html
	 */
	public function fieldIndexInput($name, $val = NULL)
	{
		$arrOptions = array(
								'none' => '---', 
								'primary' => 'PRIMARY',
								'unique' => 'UNIQUE',
								'index' => 'INDEX',
								'fulltext' => 'FULLTEXT'
						);
		
		return TableOptionsHelper::_select($name, $val, $arrOptions, true);
	}
	
	/**
	 * Function to get attributes option input
	 * @param String $name Select's name
	 * @param String $val Value to selected
	 * 
	 * @return Html
	 */
	public function fieldAttributesInput($name, $val = NULL)
	{
		$arrOptions = array('', 'BINARY', 'UNSIGNED', 'UNSIGNED ZEROFILL', 'on update CURRENT_TIMESTAMP');
		
		return TableOptionsHelper::_select($name, $val, $arrOptions);
	}
	
	/**
	 * Function to get default option input for field
	 * @param Array $name Name of input
	 * @param Array $val Value
	 * 
	 * @return Html input
	 */
	public function fieldDefaultInput($name, $val = null)
	{
		$arrOptions = array('None', 'As defined:',  'Null', 'CURRENT_TIMESTAMP');
		
		$html = TableOptionsHelper::_select($name[0], $val[0], $arrOptions);
		$html .= '<br><input type="text" name="'.$name[1].'" value="'.$val[1].'" />';
		
		return $html;
	}
	
	/**
	 * Function to create select
	 * @param String $name Name
	 * @param String $val Value to selected
	 * @param Array $options Select options
	 * @param Boolean $byArrayKey True if use Array Key as option value
	 * 
	 * @return Html
	 */
	function _select($name, $val, $options, $byArrayKey = false)
	{
		$html = '<select name="'.$name.'">' . "\n";
		
		foreach ($options as $key => $option)
		{
			$value = ($byArrayKey ? $key : $option);
			
			$selected = ($val == $value) ? 'selected="selected"' : null;
			
			$html .= '<option value="'.$value.'" '.$selected.'>'.$option.'</option>' . "\n";
		}
		
		$html .= '</select>' . "\n";
		
		return $html;
	}
	
	/**
	 * Function to generate field type input
	 * 
	 * @param String $name	Input name
	 * @param String $value	Value to selected
	 * 
	 * @return	HTML string
	 */
	public function fieldTypeInput($name, $value = NULL)
	{
		$html  = '<select name="'.$name.'">' . "\n";
		
		$options = TableOptionsHelper::fieldType();
		$selected = null;
		
		foreach ($options as $optGroup => $opt)
		{
			if(is_array($opt))
			{
				$html .= '<optgroup label="'.$optGroup.'" style="font-size: 11px;">' . "\n";
				
				$selected = ($opt) ? 'selected="selected"' : null;
				
				foreach ($opt as $option)
				{
					$selected = ($option == $value) ? 'selected="selected"' : null;
					
					$diabled = ($option == '-') ? ' disabled="disabled"' : null;
					$html .= '<option value="'.$option.'"'.$diabled.'>'.$option.'</option>' . "\n";
				}
				
				$html .= '</optgroup>' . "\n";
			}
			else
			{
				$selected = ($opt == $value) ? 'selected="selected"' : null;
				$html .= '<option value="'.$opt.'" '.$selected.'>'.$opt.'</option>' . "\n";
			}
		}
		
		$html .= '</select>' . "\n";
		
		return $html;
	}
	
	/**
	 * Function to get type of field
	 */
	public function fieldType()
	{
		$arrOptions = array(
							'INT', 'VARCHAR', 'TEXT', 'DATE',
							'NUMERIC' => array('TINYINT', 'SMALLINT', 'MEDIUMINT', 'INT', 'BIGINT',
												'-', 'DECIMAL', 'FLOAT', 'DOUBLE', 'REAL', 
												'-', 'BIT', 'BOOLEAN', 'SERIAL'),
							'DATE and TIME' => array('DATE', 'DATETIME', 'TIMESTAMP', 'TIME', 'YEAR'),
							'STRING' => array('CHAR', 'VARCHAR', 
												'-', 'TINYTEXT', 'TEXT', 'MEDIUMTEXT', 'LONGTEXT',
												'-', 'BINARY', 'VARBINARY',
												'-', 'TINYBLOB', 'MEDIUMBLOB', 'BLOB', 'LONGBLOB',
												'-', 'ENUM', 'SET'),
							'SPATIAL' => array('GEOMETRY', 'POINT', 'LINESTRING', 'POLYGON', 'MULTIPOINT',
												'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION')
						);
						
		return $arrOptions;
	}
	
	/**
	 * Function to get collation
	 */
	public function collation()
	{
		$arrOptGroup = array(
							"armscii8" => array('armscii8_bin', 'armscii8_general_ci', ),
							"ascii" => array('ascii_bin', 'ascii_general_ci', ),
							"big5" => array('big5_bin', 'big5_chinese_ci', ),
							"binary" => array('binary', ),
							"cp1250" => array('cp1250_bin', 'cp1250_croatian_ci', 'cp1250_czech_cs', 'cp1250_general_ci', 'cp1250_polish_ci', ),
							"cp1251" => array('cp1251_bin', 'cp1251_bulgarian_ci', 'cp1251_general_ci', 'cp1251_general_cs', 'cp1251_ukrainian_ci', ),
							"cp1256" => array('cp1256_bin', 'cp1256_general_ci', ),
							"cp1257" => array('cp1257_bin', 'cp1257_general_ci', 'cp1257_lithuanian_ci', ),
							"cp850" => array('cp850_bin', 'cp850_general_ci', ),
							"cp852" => array('cp852_bin', 'cp852_general_ci', ),
							"cp866" => array('cp866_bin', 'cp866_general_ci', ),
							"cp932" => array('cp932_bin', 'cp932_japanese_ci', ),
							"dec8" => array('dec8_bin', 'dec8_swedish_ci', ),
							"eucjpms" => array('eucjpms_bin', 'eucjpms_japanese_ci', ),
							"euckr" => array('euckr_bin', 'euckr_korean_ci', ),
							"gb2312" => array('gb2312_bin', 'gb2312_chinese_ci', ),
							"gbk" => array('gbk_bin', 'gbk_chinese_ci', ),
							"geostd8" => array('geostd8_bin', 'geostd8_general_ci', ),
							"greek" => array('greek_bin', 'greek_general_ci', ),
							"hebrew" => array('hebrew_bin', 'hebrew_general_ci', ),
							"hp8" => array('hp8_bin', 'hp8_english_ci', ),
							"keybcs2" => array('keybcs2_bin', 'keybcs2_general_ci', ),
							"koi8r" => array('koi8r_bin', 'koi8r_general_ci', ),
							"koi8u" => array('koi8u_bin', 'koi8u_general_ci', ),
							"latin1" => array('latin1_bin', 'latin1_danish_ci', 'latin1_general_ci', 'latin1_general_cs', 'latin1_german1_ci', 'latin1_german2_ci', 'latin1_spanish_ci', 'latin1_swedish_ci', ),
							"latin2" => array('latin2_bin', 'latin2_croatian_ci', 'latin2_czech_cs', 'latin2_general_ci', 'latin2_hungarian_ci', ),
							"latin5" => array('latin5_bin', 'latin5_turkish_ci', ),
							"latin7" => array('latin7_bin', 'latin7_estonian_cs', 'latin7_general_ci', 'latin7_general_cs', ),
							"macce" => array('macce_bin', 'macce_general_ci', ),
							"macroman" => array('macroman_bin', 'macroman_general_ci', ),
							"sjis" => array('sjis_bin', 'sjis_japanese_ci', ),
							"swe7" => array('swe7_bin', 'swe7_swedish_ci'),
							"tis620" => array('tis620_bin', 'tis620_thai_ci'),
							"ucs2" => array('ucs2_bin', 'ucs2_czech_ci', 'ucs2_danish_ci', 'ucs2_esperanto_ci', 'ucs2_estonian_ci', 'ucs2_general_ci', 'ucs2_hungarian_ci', 
												'ucs2_icelandic_ci', 'ucs2_latvian_ci', 'ucs2_lithuanian_ci', 'ucs2_persian_ci', 'ucs2_polish_ci', 'ucs2_roman_ci',
												'ucs2_romanian_ci', 'ucs2_sinhala_ci', 'ucs2_slovak_ci', 'ucs2_slovenian_ci', 'ucs2_spanish2_ci', 'ucs2_spanish_ci',
												'ucs2_swedish_ci', 'ucs2_turkish_ci', 'ucs2_unicode_ci'),
							"ujis" => array('ujis_bin', 'ujis_japanese_ci'),
							"utf16" => array('utf16_bin', 'utf16_czech_ci', 'utf16_danish_ci', 'utf16_esperanto_ci', 'utf16_estonian_ci', 'utf16_general_ci', 
												'utf16_hungarian_ci', 'utf16_icelandic_ci', 'utf16_latvian_ci', 'utf16_lithuanian_ci', 'utf16_persian_ci', 
												'utf16_polish_ci', 'utf16_roman_ci', 'utf16_romanian_ci', 'utf16_sinhala_ci', 'utf16_slovak_ci', 'utf16_slovenian_ci', 
												'utf16_spanish2_ci', 'utf16_spanish_ci', 'utf16_swedish_ci', 'utf16_turkish_ci', 'utf16_unicode_ci'),
							"utf32" => array('utf32_bin', 'utf32_czech_ci', 'utf32_danish_ci', 'utf32_esperanto_ci', 'utf32_estonian_ci', 'utf32_general_ci', 'utf32_hungarian_ci', 
												'utf32_icelandic_ci', 'utf32_latvian_ci', 'utf32_lithuanian_ci', 'utf32_persian_ci', 'utf32_polish_ci', 'utf32_roman_ci', 
												'utf32_romanian_ci', 'utf32_sinhala_ci', 'utf32_slovak_ci', 'utf32_slovenian_ci', 'utf32_spanish2_ci', 'utf32_spanish_ci', 
												'utf32_swedish_ci', 'utf32_turkish_ci', 'utf32_unicode_ci'),
							"utf8" => array('utf8_bin', 'utf8_czech_ci', 'utf8_danish_ci', 'utf8_esperanto_ci', 'utf8_estonian_ci', 'utf8_general_ci', 'utf8_hungarian_ci', 
												'utf8_icelandic_ci', 'utf8_latvian_ci', 'utf8_lithuanian_ci', 'utf8_persian_ci', 'utf8_polish_ci', 'utf8_roman_ci', 'utf8_romanian_ci', 
												'utf8_sinhala_ci', 'utf8_slovak_ci', 'utf8_slovenian_ci', 'utf8_spanish2_ci', 'utf8_spanish_ci', 'utf8_swedish_ci', 
												'utf8_turkish_ci', 'utf8_unicode_ci'),
							"utf8mb4" => array('utf8mb4_bin', 'utf8mb4_czech_ci', 'utf8mb4_danish_ci', 'utf8mb4_esperanto_ci', 'utf8mb4_estonian_ci', 'utf8mb4_general_ci', 
													'utf8mb4_hungarian_ci', 'utf8mb4_icelandic_ci', 'utf8mb4_latvian_ci', 'utf8mb4_lithuanian_ci', 'utf8mb4_persian_ci', 
													'utf8mb4_polish_ci', 'utf8mb4_roman_ci', 'utf8mb4_romanian_ci', 'utf8mb4_sinhala_ci', 'utf8mb4_slovak_ci', 'utf8mb4_slovenian_ci', 
													'utf8mb4_spanish2_ci', 'utf8mb4_spanish_ci', 'utf8mb4_swedish_ci', 'utf8mb4_turkish_ci', 'utf8mb4_unicode_ci'),
						);
		
		return $arrOptGroup;
	}
}